package com.leyou.item.web;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.pojo.Sku;
import com.leyou.item.pojo.Spu;
import com.leyou.item.pojo.SpuDetail;
import com.leyou.item.service.GoodsService;
import com.leyou.item.vo.SpuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spu")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("page")
    public ResponseEntity<PageResult<SpuVo>> querySpuBoByPage(
                            @RequestParam(value = "key", required = false)String key,
                            @RequestParam(value = "saleable", required = false)Boolean saleable,
                            @RequestParam(value = "page", defaultValue = "1")Integer page,
                            @RequestParam(value = "rows", defaultValue = "10")Integer rows){
        PageResult<SpuVo> pageResult = this.goodsService.querySpuBoByPage(key, saleable, page, rows);
        return ResponseEntity.ok(pageResult);
    }

    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuVo spuVo){
        this.goodsService.saveGoods(spuVo);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("detail/{spuId}")
    public ResponseEntity<SpuDetail> querySpuDetailBySpuId(@PathVariable("spuId") Long spuId){
        return ResponseEntity.ok(this.goodsService.querySpuDetailBySpuId(spuId));
    }

    @GetMapping("list")
    public ResponseEntity<List<Sku>> querySkuBySpuId(@RequestParam("id") Long spuId){
        return ResponseEntity.ok(this.goodsService.querySkuBySpuId(spuId));
    }

    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuVo spuVo){
        this.goodsService.updateGoods(spuVo);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @PutMapping("removal/{spuId}")
    public ResponseEntity<Void> removalGoods(@PathVariable("spuId") Long spuId, @RequestParam("saleable") Boolean saleable){
        this.goodsService.removalGoods(spuId, saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @DeleteMapping("delete/{spuId}")
    public ResponseEntity<Void> deleteGoods(@PathVariable("spuId") Long spuId){
        this.goodsService.deleteGoods(spuId);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("{id}")
    public ResponseEntity<Spu> querySpuById(@PathVariable("id") Long id){
        return ResponseEntity.ok(this.goodsService.querySpuById(id));
    }

    @GetMapping("sku/{id}")
    public ResponseEntity<Sku> querySkuById(@PathVariable("id")Long id){
        return ResponseEntity.ok(this.goodsService.querySkuById(id));
    }
}
